//
//  RegisterView.swift
//  shop
//
//  Created by chen on 2025/5/30.
//

import SwiftUI
import ToastUI

struct RegisterView: View {
    
    @EnvironmentObject private var authManager: AuthManager
    
    @EnvironmentObject private var router: Router
    
    @State private var phone: String = ""
    
    @State private var password: String = ""
    
    @State private var isSecured: Bool = true
    
    @State private var registerLoading: Bool = false
    
    var body: some View {
        ZStack{
            VStack(spacing: 0) {
                ZStack{
                    Text("注册")
                        .font(.system(size: 17))
                        .bold()
                        .padding(.vertical, 10)
                    
                    HStack(spacing: 0){
                        Button {
                            router.back()
                        } label: {
                            Image(systemName: "chevron.left")
                                .foregroundColor(.black)
                                .font(.system(size: 18))
                        }

                        Spacer()
                    }
                    .padding(.horizontal, 10)
                    .padding(.vertical, 10)
                }
                .background(.white)
                
                HStack{
                    Image("logo")
                        .resizable()
                        .aspectRatio(contentMode: .fit)
                        .frame(width: 60, height: 60)
                }
                .padding(.top, 30)
                .padding(.bottom, 40)
                
                VStack(alignment: .center, spacing: 0) {
                    VStack(spacing: 0) {
                        HStack(spacing: 15) {
                            Image(systemName: "phone.fill")
                                .font(.system(size: 17))
                                .foregroundStyle(Color.black)
                            
                            TextField("请输入手机号码", text: $phone)
                                .textFieldStyle(.plain)
                                .font(.system(size: 15))
                                .keyboardType(.numberPad)
                                .modifier(DismissKeyboardOnTap())
                                .tint(.red)
                                .kerning(1.5)
                                
                            Spacer()
                            
                            if !phone.isEmpty {
                                Button(action: {
                                    phone = ""
                                }) {
                                    Image(systemName: "xmark")
                                        .resizable()
                                        .scaledToFit()
                                        .frame(width: 12, height: 12)
                                        .foregroundStyle(Color.black)
                                }
                            }
                        }
                        .padding(.bottom, 10)
                        .padding(.horizontal, 8)
                        Divider()
                    }
                    
                    VStack(spacing: 0) {
                        HStack(spacing: 15) {
                            Image(systemName: "lock.fill")
                                .font(.system(size: 17))
                                .foregroundStyle(Color.black)
                            
                            if isSecured {
                                SecureField("请输入密码", text: $password)
                                    .textFieldStyle(.plain)
                                    .font(.system(size: 15))
                                    .keyboardType(.default)
                                    .modifier(DismissKeyboardOnTap())
                                    .tint(.red)
                            } else {
                                TextField("请输入密码", text: $password)
                                    .textFieldStyle(.plain)
                                    .font(.system(size: 15))
                                    .keyboardType(.default)
                                    .modifier(DismissKeyboardOnTap())
                                    .tint(.red)
                            }
                                
                            Spacer()
                            
                            Button(action: {
                                isSecured.toggle()
                            }) {
                                Image(systemName: isSecured ? "eye.slash" : "eye")
                                    .font(.system(size: 14))
                                    .foregroundStyle(Color.black)
                            }
                        }
                        .padding(.bottom, 10)
                        .padding(.horizontal, 8)
                        Divider()
                    }
                    .padding(.top, 20)
                    
                    Button {
                        doRegister()
                    } label: {
                        HStack{
                            Text("注册")
                                .font(.system(size: 16))
                                .foregroundStyle(.white)
                            
                            if registerLoading {
                                ProgressView()
                                    .progressViewStyle(CircularProgressViewStyle(tint: .white))
                            }
                            
                        }
                        .padding(10)
                        .frame(maxWidth: .infinity)
                        .background(Color.red)
                        .cornerRadius(5)
                        .padding(.top, 40)
                    }
                }
                .padding(.vertical, 10)
                .padding(.horizontal, 40)
                
                Spacer()
            }
        }
        .navigationBarBackButtonHidden(true)
    }
    
    private func doRegister() {
        if phone.isEmpty {
            ToastAsync("手机号不能为空")
            return
        }
        
        if password.isEmpty {
            ToastAsync("密码不能为空")
            return
        }
        
        Task {
            registerLoading = true
            let param: RegisterReq = RegisterReq(phone: phone, password: password.md5)
            
            await NetManager.server.httpPost(.register, bodyParam: param) { (data: RegisterResp) in
                self.authManager.login(phone: data.phone, token: data.token)
                self.router.to(.contentView(selectTab: .mine))
            }
            registerLoading = false
        }
    }
}

#Preview {
    RegisterView()
}
